/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#include "MkHttp/MkHttpClient.h"
#include "MkEvent/MkEventPool.h"
#include "MkEvent/MkTimer.h"
#include "MkUtil/MkLog.h"

int main(int argc, char ** argv)
{
#ifdef _WIN32
    WSADATA WSAData;
    WSAStartup(0x101, &WSAData);
#endif
    MkLogChangeLevel(LOG_DEBUG);
    CMkEventPool Pool;
    Uint32 ErrorCode = Pool.Start();
    if (NoneError != ErrorCode) {
        MkErrorLog("start event pool failed\n");
        return 0;
    }
    CMkTimer Timer;
    CMkHttpClient pClient(Pool.GetFreeEvent(), &Timer,nullptr);
    Timer.StartTimerThread();
    char ch;
    BOOL bQuit = FALSE;
    while (ch = getchar()) {
        switch (ch) {
        case '1':
            pClient.WebSocketConnect("ws://192.168.100.120:50080/web/16920707117278707729@16920707111328729040.live.flv", []() {
                MkDebugLog("websocket connected\n");
            }, [](const Uint8* pBuf, Uint32 nLen) {
                static FILE *fp = nullptr;
                if (!fp) {
                    fp = fopen("C:/Workspace/media/aa.recv.flv.dat", "wb");

                }
                fwrite(pBuf, 1, nLen, fp);
                MkDebugLog("websocket recv body\n");
            }, []() {
                MkDebugLog("websocket closed\n");
            });
            break;
        case 'q':
            bQuit = TRUE;
            break;
        default:
            break;
        }
        if (bQuit) {
            break;
        }
    }
    Timer.StopTimerThread();
    Pool.Stop();
#ifdef _WIN32
    WSACleanup();
#endif
    return 0;
}